package com.itheima.web;

import cn.hutool.core.lang.UUID;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/7-18:51
 * @Version: 1.0.0
 */
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private JedisPool jedisPool;

    @Reference(interfaceClass = SetmealService.class)
    private SetmealService setmealService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        Jedis jedis = jedisPool.getResource();
        try{
//        获取原始文件名称
        String originalFilename = imgFile.getOriginalFilename();
//        获取最后分割的元素序号
        int  lastIndex = originalFilename.lastIndexOf(".");
//        获取后缀
        String suffix = originalFilename.substring(lastIndex);
//        拼接
        String fileName = UUID.randomUUID().toString()+suffix;
//        调用上传工具类
        QiniuUtil.upload(imgFile.getBytes(),fileName);
//        如果上传成功将图片上传到redis数据库
        jedis.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
//        返回结果
        return Result.OK("",fileName);
        }catch (Exception e){
            return Result.ERROR(MessageConstant.PIC_UPLOAD_FAIL);
        }finally {
            jedis.close();
        }
    }

    /**
     * 添加套餐功能
     * @Param: 接受前端发送过来的封装好的套餐对象 , 用RequestBody接受
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setmeal){
        try{
//            调用服务完成添加套餐功能
            setmealService.add(setmeal);
            return Result.OK(MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            return Result.ERROR(MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
//       调用service的方法完成分页查询
        PageResult pageResult = setmealService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return pageResult;

    }

    @RequestMapping("/findById")
    public Result findById(String setmealId){
//                formData: {},//表单数据
//                tableData:[],//添加表单窗口中检查组列表数据
//                checkgroupIds:[],//添加表单窗口中检查组复选框对应id
//        编辑页面需要的数据用Map集合存放
        Map map = setmealService.findById4Edit(setmealId);
        return Result.OK("",map);
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result edit(@RequestBody Setmeal setmeal){
        try{
//            调用服务完成添加套餐功能
            setmealService.edit(setmeal);
            return Result.OK("编辑套餐成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR("编辑套餐失败");
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result delete(Integer setmealId){
        try{
            setmealService.delete(setmealId);
            return Result.OK(MessageConstant.DELETE_CHECKGROUP_SUCCESS);

        } catch (Exception e){
            return Result.ERROR(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }
}
